from urllib import request
from config import config_qywx as config
from utils import logging
import sys, json, logging

#获取AccessToken
def get_weather_access_token():
    QYWX_ACCESS_TOKEN_URL = config.QYWX_ACCESS_TOKEN_URL
    QYWX_ACCESS_TOKEN_URL = str(QYWX_ACCESS_TOKEN_URL).replace("${corpid}", config.QYWX_CORP_ID)
    QYWX_ACCESS_TOKEN_URL = str(QYWX_ACCESS_TOKEN_URL).replace("${corpsecret}", config.QYWX_WEATHER_SECRET)
    req = request.Request(QYWX_ACCESS_TOKEN_URL)
    try:
        resp = request.urlopen(req)
    except request.HTTPError as e:
        logging.exception(e.msg, e.args)
        sys.exit(1)
    access_token_dictionary = json.load(resp)
    logging.info(access_token_dictionary)
    return access_token_dictionary


#获取AccessToken
def get_news_access_token():
    QYWX_ACCESS_TOKEN_URL = config.QYWX_ACCESS_TOKEN_URL
    QYWX_ACCESS_TOKEN_URL = str(QYWX_ACCESS_TOKEN_URL).replace("${corpid}", config.QYWX_CORP_ID)
    QYWX_ACCESS_TOKEN_URL = str(QYWX_ACCESS_TOKEN_URL).replace("${corpsecret}", config.QYWX_NEWS_SECRET)
    req = request.Request(QYWX_ACCESS_TOKEN_URL)
    try:
        resp = request.urlopen(req)
    except request.HTTPError as e:
        logging.exception(e.msg, e.args)
        sys.exit(1)
    access_token_dictionary = json.load(resp)
    logging.info(access_token_dictionary)
    return access_token_dictionary
